package com.core.controller;

import com.core.domain.DTO.LygStadiumDTO;
import com.core.domain.User;
import com.core.service.DjsService;
import com.core.service.LygService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author 刘玉贵
 * @version 666
 */

@CrossOrigin(origins = {"http://localhost:8080", "http://localhost:63343"})
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private LygService lygService;

    @Autowired
    private DjsService djsService;

    @GetMapping
    String selectAllStadium(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        LinkedList<LygStadiumDTO> inf = new LinkedList<>();
        //获取所有场馆基础信息
        List<LygStadiumDTO> stadiums = lygService.selectAllStadium();
        for (LygStadiumDTO stadium : stadiums) {
            Integer teamId = stadium.getId();
            //查询已经被预约了的数量
            int count = lygService.countById(teamId);
            stadium.setBookedNum(count);
            //查询当前用户是否已经预约

            //获取登陆用户的id
            Integer userId = (Integer) request.getSession().getAttribute("userId");
            Integer isBooked = lygService.isBooked(userId, teamId); //当预约表没有信息的时候,返回为0
            stadium.setUserId(userId);
            stadium.setIsBooked(isBooked);
            inf.add(stadium);
            System.out.println("场馆id" + teamId + "已经被预约的数量" + count + "用户id" + userId + "该用户是否预约该场馆" + isBooked);
        }

        request.setAttribute("inf", inf);
        request.getRequestDispatcher("/reservation_page/reservation.jsp").forward(request, response);
        return null;
    }

    @PostMapping("/reserve")
    void reserve(HttpServletRequest request, HttpServletResponse response, @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm") Date start, @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm") Date end, @RequestParam int teamId, @RequestParam int userId,@RequestParam int reservedNumber) throws ServletException, IOException, InterruptedException {
        // 创建一个SimpleDateFormat对象，指定时间格式为yyyy-MM-dd HH:mm:ss
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");

        String start1 = sdf.format(start);
        String end1 = sdf.format(end);

        lygService.insertResInf(Timestamp.valueOf(start1), Timestamp.valueOf(end1), teamId, userId,reservedNumber);
        response.sendRedirect("/stadium/user");
        System.out.println(Timestamp.valueOf(start1) + " " + Timestamp.valueOf(end1) + " " + teamId + " " + userId + " " + reservedNumber);
    }

    @PostMapping("/cancel")
    void cancel(HttpServletRequest request, HttpServletResponse response, Integer id, Integer userId) throws IOException, ServletException {
        System.out.println("cancel " + userId + " " + id);
        lygService.delete(userId, id);
//        request.getRequestDispatcher("/stadium/user").forward(request,response);
        response.sendRedirect("/stadium/user");
    }

    @GetMapping("/inf")
    User getInf(HttpServletRequest request, HttpServletResponse response){
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        User user = lygService.getInf(userId);
        return user;
    }

    @PostMapping("getPrevious")
    public List<Map<String,String>> getPrevious(HttpServletRequest request) {
        System.out.println(111);
        Object userId = request.getSession().getAttribute("userId");
        List<Map<String,String>> data = djsService.getPrevious(userId);
        return data;
    }
    @GetMapping("updatePraise")
    public List<Map<String,String>> updatePopular(@RequestParam("orderId") String orderId,@RequestParam("praiseOption")String praiseOption,HttpServletRequest request){
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        List<Map<String,String>> data = djsService.updatePopular(userId,orderId,praiseOption);
        return data;
    }

}
